﻿using MediatR;
using W.Device.Domain.DTO;
using W.Purchase.Domain;
using W.Purchase.Domain.DTO;
using W.Purchase.Read.API.AppLication.Command;
using W.Purchase.Repository.Interface;

namespace W.Purchase.Read.API.AppLication.CommandHandler
{
    public class VendorNameShowCommandHandler : IRequestHandler<VendorNameShowCommand, ApiResult<List<Vendor>>>
    {
        private readonly IVendorRepository _vendorRepository;
        private readonly ILogger<VendorNameShowCommandHandler> _logger;
        public VendorNameShowCommandHandler(IVendorRepository vendorRepository, ILogger<VendorNameShowCommandHandler> logger)
        {
            _vendorRepository = vendorRepository;
            _logger = logger;
        }
        public async Task<ApiResult<List<Vendor>>> Handle(VendorNameShowCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<Vendor>> result = new ApiResult<List<Vendor>>();
            try
            {
                _logger.LogInformation("显示供应商名称成功");
                var list = await _vendorRepository.GetAsync(x => x.IsDelete == false);
                result.Data = list;
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
                throw;
            }
            return result;
        }
    }
}
